package com.ccloud.gen.action.config;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.ccloud.common.core.utils.file.FileUtils;
import com.ccloud.gen.engine.SimpleTemplateEngine;
import com.ccloud.gen.engine.base.GunsTemplateEngine;
import com.ccloud.gen.engine.config.ContextConfig;
import com.ccloud.gen.engine.config.SqlConfig;

import java.io.File;
import java.util.List;


/**
 * @Version V1.0
 * @Project bigdata-cloud
 * @Title AbstractGeneratorConfig
 * @Description 代码生成的抽象配置
 * @Package com.ccloud.gen.action.config
 * @Date 2021/7/8 3:28 下午
 * @Copyright: 武汉大数据产业发展有限公司 All rights reserved.
 */
public abstract class AbstractGeneratorConfig {

    final static String BUSINESS_NAME = "modules";

    public static final String CHENTL = "chentl";

    public static final String MODULE_NAME = "file";

    /**
     * mybatis-plus代码生成器配置
     */
    GlobalConfig globalConfig = new GlobalConfig();

    DataSourceConfig dataSourceConfig = new DataSourceConfig();

    StrategyConfig strategyConfig = new StrategyConfig();

    PackageConfig packageConfig = new PackageConfig();

    TableInfo tableInfo = null;

    /**
     * Guns代码生成器配置
     */
    ContextConfig contextConfig = new ContextConfig();

    SqlConfig sqlConfig = new SqlConfig();

    protected abstract void config();

    public void init() {
        config();
        packageConfig.setService(contextConfig.getProPackage() + "." + MODULE_NAME + "." + contextConfig.getModuleName() + ".service");
        packageConfig.setServiceImpl(contextConfig.getProPackage() + "." + MODULE_NAME + "." + contextConfig.getModuleName() + ".service.impl");

        //controller没用掉,生成之后会自动删掉
        packageConfig.setController(CHENTL);

        if (!contextConfig.getEntitySwitch()) {
            packageConfig.setEntity(CHENTL);
        }

        if (!contextConfig.getDaoSwitch()) {
            packageConfig.setMapper(CHENTL);
            packageConfig.setXml(CHENTL);
        }

        if (!contextConfig.getServiceSwitch()) {
            packageConfig.setService(CHENTL);
            packageConfig.setServiceImpl(CHENTL);
        }

    }

    /**
     * 删除不必要的代码
     */
    public void destory() {
        String outputDir = globalConfig.getOutputDir() + "/chentl";
        FileUtils.deleteDirectory(new File(outputDir));
    }

    public AbstractGeneratorConfig() {
    }

    public void doMpGeneration() {
        init();
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.execute();
        destory();

        //获取table信息,用于guns代码生成
        List<TableInfo> tableInfoList = autoGenerator.getConfig().getTableInfoList();
        if (tableInfoList != null && tableInfoList.size() > 0) {
            this.tableInfo = tableInfoList.get(0);
        }
    }

    public void doGunsGeneration() {
        GunsTemplateEngine GunsTemplateEngine = new SimpleTemplateEngine();
        GunsTemplateEngine.setContextConfig(contextConfig);
        sqlConfig.setConnection(dataSourceConfig.getConn());
        GunsTemplateEngine.setSqlConfig(sqlConfig);
        GunsTemplateEngine.setTableInfo(tableInfo);
        GunsTemplateEngine.start();
    }
}
